package com.df.bpm.utils.businessViewerV2.informatization;


import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class V_details extends JPanel {

    int unit;
    V_scroll4Reason scrReasons=new V_scroll4Reason();
    V_scroll4Target scrTargets=new V_scroll4Target();
    V_scroll4Output scrOutputs=new V_scroll4Output();

    MouseAdapter adapter;

    /**
     * 监控点击范围的玻璃板
     */
    JPanel glassPane;

    M_requirementDetailsDto detailsDto;

    public V_details(JLayeredPane layeredPane, int unit) {
        this.unit=unit;
        this.setSize(layeredPane.getWidth()-13*unit, layeredPane.getHeight());
        this.setLocation(13*unit, 0);
        this.setLayout(null);
        int scrWidth=(this.getWidth()-2*unit)/3;
        scrReasons.setLocation(unit, unit);
        int scrHeight=this.getHeight()-4*unit-scrReasons.getY();
        scrReasons.setSize(scrWidth,scrHeight);
        this.add(scrReasons);
        scrTargets.setSize(scrWidth,scrHeight);
        scrTargets.setLocation(scrReasons.getX()+scrReasons.getWidth(), scrReasons.getY());
        this.add(scrTargets);
        scrOutputs.setSize(scrWidth,scrHeight);
        scrOutputs.setLocation(scrTargets.getX()+scrTargets.getWidth(), scrTargets.getY());
        this.add(scrOutputs);
        this.setBackground(new Color(230, 230, 230));
        this.setOpaque(true);  // 确保面板是不透明的
        layeredPane.add(this);
        layeredPane.setLayer(this, JLayeredPane.DEFAULT_LAYER+2);
        addMouseListenerToLayeredPane(layeredPane);
        layeredPane.revalidate();
        layeredPane.repaint();
    }

    /**
     * 鼠标点击在V_details之外时隐藏V_details
     * @param layeredPane   V_details的容器
     */
    public void addMouseListenerToLayeredPane(JLayeredPane layeredPane) {
        if(glassPane!=null){
            layeredPane.remove(glassPane);
        }else{
            glassPane=new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    // 这里可以添加额外的绘制逻辑，但现在保持透明
                    g.setColor(new Color(0, 0, 0, 0)); // 完全透明
                    g.fillRect(0, 0, getWidth(), getHeight());
                }
            };
            glassPane.setLayout(null); // 通常不需要设置布局，因为我们是覆盖整个 layeredPane
            glassPane.setBounds(0, 0, layeredPane.getWidth(), layeredPane.getHeight());
            glassPane.setOpaque(false);

            adapter=new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    // 检查鼠标点击位置是否在 V_details 之外
                    System.out.println(e.getPoint());
                    if (e.getX()<V_details.this.getX()) {
                        // 隐藏 V_details（可以从 layeredPane 中移除它，或者设置其可见性为 false）
                        V_details.this.setVisible(false);
                        layeredPane.revalidate();
                        layeredPane.repaint();
                    }
                }
            };
        }
        glassPane.addMouseListener(adapter);
        layeredPane.add(glassPane);
        // 将玻璃面板添加到 layeredPane 的默认层+1层，第+2层放details面板
        layeredPane.setLayer(glassPane, JLayeredPane.DRAG_LAYER+1);
        layeredPane.setLayer(this, JLayeredPane.DRAG_LAYER+2);
    }

    public void setVisible(boolean isVisible){
        if(glassPane!=null){
            glassPane.setVisible(isVisible);
        }
        super.setVisible(isVisible);
    }

    public void putModel(M_requirementDetailsDto detailsDto) {
        this.detailsDto=detailsDto;
        scrReasons.setRequirementBasic(detailsDto.getBasic());
        scrReasons.putModel(detailsDto.getReasons());
        scrTargets.setRequirementBasic(detailsDto.getBasic());
        scrTargets.putModel(detailsDto.getTargets());
        scrOutputs.setRequirementBasic(detailsDto.getBasic());
        scrOutputs.putModel(detailsDto.getOutputs());
    }


}
